# Copyright (C) 2007 by Sergio Pistone
# sergio_pistone@yahoo.com.ar
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

require "test/unit/testsuite"
require "test/unit/ui/console/testrunner"
require "plugin_tests"

$LOG_FILE = "#{ENV["HOME"]}/.wikilyrics.log"

class PluginsTests

	def self.find_plugins( plugin_names )
		plugins = {}
		Dir.new( File.expand_path( File.dirname( __FILE__ ) + "/.." ) ).each() do |filename|
			filename = File.expand_path( File.dirname( __FILE__ ) + "/../" + filename )
			if File.file?( filename ) && (md = /\/lyrics_([A-Za-z_0-9]*)\.rb$/.match( filename ))
				next if plugin_names && ! plugin_names.include?( md[1] )
				plugins[md[1]] = filename
			end
		end
		return plugins
	end

	def self.load_plugins( plugins )
		@plugins = []
		plugins.each() do |classname, filename|
			require( filename ? filename : File.expand_path( File.dirname( __FILE__ ) + "/../lyrics_#{classname}.rb" ) )
			classobj = eval( classname )
			@plugins << eval( "#{classname}.new()" )
		end
		@plugins.sort!() { |x,y| x.site_name() <=> y.site_name() }
	end

	def self.suite( plugin_names, cases )
		suite = Test::Unit::TestSuite.new( "Lyrics Plugins Tests" )
		plugin_names = plugin_names ? plugin_names.split( "," ) : nil
		if plugin_names && plugin_names.size == 1 && cases
			cases = cases.split( "," )
			cases.size.times { |index| cases[index] = cases[index].to_i - 1 }
			cases = nil if cases.empty?
		end
		plugins = self.load_plugins( self.find_plugins( plugin_names ) )
		if plugins.empty?
			puts "No valid plugin to test selected..."
		else
			logger = Logger.new( $LOG_FILE, 0 )
			plugins.each() { |plugin| plugin.logger = logger }
			plugins.each() { |plugin| suite << PluginTests.build_suite( plugin, cases ) }
		end
		return suite
	end

end

if $0 == __FILE__

	Test::Unit::UI::Console::TestRunner.run( PluginsTests.suite( ARGV[0], ARGV[1]) )

end
